<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item :label="$t('key.ssyx')" >
              <a-select allowClear showSearch v-model="queryParam.kcyx" :filterOption="filterOption">
                <a-select-option v-for="(i, index) in selectMenu.yx" :key="index" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item :label="$t('key.kkyx')" >
              <a-select allowClear v-model="queryParam.kcdw" showSearch @change="yxChange" :filterOption="filterOption">
                <a-select-option v-for="(i, index) in selectMenu.yx" :key="index" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item :label="$t('key.kcmc')" >
              <a-select v-model="queryParam.kcdm" @change="kcChange" allowClear showSearch :filterOption="filterOption">
                <a-spin v-if="kcLoading" slot="notFoundContent" size="small"/>
                <a-select-option v-for="(i, index) in selectMenu.courseList" :key="index" :value="i.kch">{{ i.kch + '-' + i.kcmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item :label="$t('key.ksnj')" >
                <a-select allowClear v-model="queryParam.ksnj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item :label="$t('key.ksxn')" >
                <a-select allowClear v-model="queryParam.ksxn">
                  <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item :label="$t('key.ksxq')" >
                <a-select allowClear v-model="queryParam.ksxq">
                  <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item :label="$t('key.ksxz')" >
                <a-select allowClear v-model="queryParam.ksxz">
                  <a-select-option v-for="i in TYPE0358" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item :label="$t('key.kslb')" >
                <a-select allowClear v-model="queryParam.kslb">
                  <a-select-option v-for="i in TYPE0109" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item :label="$t('key.cjlx')" >
                <a-select allowClear v-model="queryParam.cjlxm">
                  <a-select-option v-for="i in TYPE0328" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item :label="$t('key.xh')" >
                <a-input v-model="queryParam.xh"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item :label="$t('key.name')" >
                <a-input v-model="queryParam.xm"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item :label="$t('key.xjzt')">
                <a-select allowClear v-model="queryParam.xjztm" placeholder="请选择">
                  <a-select-option v-for="i in TYPE0073" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>

          <a-col :md="12" :sm="24">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">{{ $t('button.query') }}</a-button>
              <a-button type="primary" @click="handleExport" style="margin:0 10px">{{ $t('button.export') }}</a-button>
			 
              <a-button style="margin: 0 10px" @click="handleReset">{{ $t('button.reset') }}</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? $t('button.packUp') : $t('button.advance') }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>
    
    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        ref="table"
        size="default"
        rowKey="xsxh"
        :columns="columns"
        :data="loadData"
        :pagination="{ pageSize: 10 }"
        :scroll="{ x: 2300 }"
      >
        <template slot="cjlxm" slot-scope="a, b">
          {{ b.cjlxm | filterCjlxm(TYPE0328) }}
        </template>
        <template slot="cjzt" slot-scope="a, b">
          {{ b.cjzt | filterCjzt(TYPE0127) }}
        </template>
        <template slot="action" slot-scope="id, record">
          <a @click="showScore(record)">查看</a>
        </template>
      </s-table>
    </a-card>
    
    <creditModal
      ref="check"
      :visible="creditVisible"
      @cancel="creditVisible = false"
    />
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import creditModal from './creditModal'

export default {
  name: 'grade-query',
  data() {
    return {
      form: this.$form.createForm(this),
      selectMenu: {},
      kcLoading: false,
      advanced: true,
      queryParam: {
        // xjztm: '1'
      },
      columns: [
        // {
        //   title: '序号',
        //   scopedSlots: { customRender: 'serial' }
        // },
        {
          title: this.$t('key.xh'),
          dataIndex: 'xsxh',
          width: "170px"
        },
        {
          title: this.$t('key.name'),
          dataIndex: 'xm',
          width: "140px"
        },
        {
          title: this.$t('key.yx'),
          dataIndex: 'kkyxmc',
        },
        {
          title: this.$t('key.zy'),
          dataIndex: 'sszymc',
        },
        {
          title: this.$t('key.nj'),
          dataIndex: 'ksnj',
          width: "80px"
        },
        {
          title: this.$t('key.kcdm'),
          dataIndex: 'kcbm',
          width: "120px"
        },
        {
          title: this.$t('key.xn'),
          dataIndex: 'ksxn',
          width: "120px"
        },
        {
          title: this.$t('key.xq'),
          dataIndex: 'ksxqmc',
          width: "120px"
        },
        {
          title: this.$t('key.kcmc'),
          dataIndex: 'kcmc',
          width: "200px"
        },
        {
          title: this.$t('key.ksxz'),
          dataIndex: 'ksxzmc',
          width: "100px"
        },
        {
          title: this.$t('key.kslb'),
          dataIndex: 'kslbmc',
          width: "100px"
        },
        {
          title: this.$t('key.kclb'),
          dataIndex: 'kclbmc',
          width: "100px"
        },
        {
          title: this.$t('key.xkyq'),
          dataIndex: 'bxhxxmc',
          width: "80"
        },
        {
          title: this.$t('key.sfxwk'),
          dataIndex: 'sfxwkmc',
          width: "80"
        },
        {
          title: this.$t('key.cj'),
          dataIndex: 'kscj',
          width: "100px"
        },
        {
          title: this.$t('key.xf'),
          dataIndex: 'xf',
          width: "80"
        },
        {
          title: this.$t('key.cjlx'),
          // dataIndex: 'cjlxm',
          scopedSlots: { customRender: 'cjlxm' },
          width: "120px"
        },
        {
          title: this.$t('key.cjzt'),
          // dataIndex: 'cjzt',
          scopedSlots: { customRender: 'cjzt' },
          width: "120px"
        },
        {
          title: this.$t('key.note'),
          dataIndex: 'note',
          width: "120px"
        },
        {
          title: '操作',
          scopedSlots: { customRender: 'action' },
          fixed: 'right',
          width: "140px"
        }
      ],
      loadData: parameter => {
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.kccjXsList(params).then(res => {
          // console.log(res)
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      creditVisible: false,
      // selectedRowKeys: [],
      // options: {
      //   alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        // rowSelection: {
        //   selectedRowKeys: this.selectedRowKeys,
        //   onChange: this.onSelectChange
        // }
      // },
    }
  },
  components: { STable, creditModal },
  computed: {
    ...mapState({
      TYPE0072: state => state.app['TYPE0072'], // 学期
      TYPE0125: state => state.app['TYPE0125'], // 学年
      TYPE0126: state => state.app['TYPE0126'], // 年级
      TYPE0109: state => state.app['TYPE0109'], // 考试类别
      TYPE0359: state => state.app['TYPE0359'], // 课程代码
      TYPE0358: state => state.app['TYPE0358'], // 课程性质
      TYPE0127: state => state.app['TYPE0127'], // 成绩状态
      TYPE0328: state => state.app['TYPE0328'], // 成绩类型
      TYPE0073: state => state.app['TYPE0073'],
    })
  },
  created () {
    this.initSelectMenu()
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0072')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0109')
    this.$store.dispatch('app/setDictionary', 'TYPE0359')
    this.$store.dispatch('app/setDictionary', 'TYPE0358')
    this.$store.dispatch('app/setDictionary', 'TYPE0127')
    this.$store.dispatch('app/setDictionary', 'TYPE0328')
    this.$store.dispatch('app/setDictionary', 'TYPE0073')
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$nextTick(() => {
        this.$set(this.queryParam, 'kcdm', '')
        this.form.setFieldsValue({kcbm: ''})
        this.$set(this.selectMenu, 'courseList', [])
        if (code) {
          this.kcLoading = true
          this.$api.cultivate.courseAll({kcssyxm: code}).then(res => {
            this.$set(this.selectMenu, 'courseList', res.data)
          }).finally(() => {
            this.kcLoading = false
          })
        } else {
          this.$set(this.selectMenu, 'courseList', [])
        }
      })
    },
    kcChange (kch) {
      this.$nextTick(() => {
        let kc = this.selectMenu.courseList.find(kc => {
          return kc.kch === kch
        })
        this.form.setFieldsValue({
          xs: kc.zxs,
          xf: kc.xf
        })
      })
    },
    // onSelectChange (selectedRowKeys) {
    //   this.selectedRowKeys = selectedRowKeys
    // },
    handleReset() {
      this.queryParam = {}
      // this.$refs.table.selectedRowKeys = []
    },
    handleExport() {
         let params = this.queryParam;
		 console.log(111);
      this.$export('/teacherClient/kccj/xsListExport', {
        ...params
      })
    
    },
    showScore (data) {
      this.creditVisible = true
      this.$refs.check.detail = data
    },
  },
  filters: {
    filterCjlxm(value, type) {
      let name = null
      type.map(item => {
        if(item.code === value) return name = item.name
      })
      return name
    },
    filterCjzt(value, type) {
      let name = null
      type.map(item => {
        if(item.code === value) return name = item.name
      })
      return name
    }
  }
}
</script>


